from PIL import Image


def convert_png_to_ico(png_path, ico_path, size):
    """透明通道PNG转ICO格式核心函数

    Args:
        原图路径 (str): 透明背景PNG文件路径（如：'logo.png'）
        图标路径 (str): 输出ICO文件路径（如：'favicon.ico'）
        尺寸 (tuple): 图标分辨率，推荐(256,256)或(128,128)
    """
    try:
        # Load the PNG image
        image = Image.open(png_path)

        # Resize the image to the specified size
        image = image.resize(size, Image.LANCZOS)

        # Save the resized image as an ICO file
        image.save(ico_path, format="ICO")
        print(f"Successfully converted {png_path} to {ico_path}")
    except Exception as e:
        print(f"An error occurred: {e}")


# 示例
png_file = "./app.png"  # 替换为你的PNG图片路径

ico_file = r"./app.ico"  # 替换为你想保存的ICO文件路径
desired_size = (256, 256)  # 设置你想要的图标大小，例如256x256像素

convert_png_to_ico(png_file, ico_file, desired_size)
